package com.hzhc.controller.sysmanage;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzhc.annotation.SysTemLog;
import com.hzhc.common.BaseController;
import com.hzhc.dto.UserDto;
import com.hzhc.entity.SysDict;
import com.hzhc.entity.SysDictType;
import com.hzhc.enumBean.ResultUtilCodeEnum;
import com.hzhc.mapper.SysDictTypeMapper;
import com.hzhc.service.SysDictService;
import com.hzhc.service.SysDictTypeService;
import com.hzhc.utils.ResultUtil;
import com.hzhc.utils.TokenUtil;
import com.hzhc.vo.SysDictTypeVo;
import com.hzhc.vo.SysDictVo;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 字典管理
 * @author 功不唐捐
 * @since 2024-04-07
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/sysDictType")
public class SysDictTypeController extends BaseController {

    private final SysDictTypeService dictTypeService;
    private final SysDictService dictService;
    private final SysDictTypeMapper sysDictTypeMapper;


    /**
     * 字典分页查询
     *
     * @param vo 查询字段
     * @return 查询结果
     */
    @RequestMapping("/getTypeList")
    public ResultUtil<List<SysDictTypeVo>> getDictTypeList(@RequestBody SysDictTypeVo vo) {
        Page<SysDictTypeVo> page = new Page<>(vo.getPageIndex(), vo.getPageSize());
        List<SysDictTypeVo> types = dictTypeService.getSysDictTypeVoList(page, vo);
        return ResultUtil.build(types, page.getTotal(), page.getPages(), ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 字典新增
     *
     * @param type 字典值
     * @return 编辑结果
     */
    @PostMapping("/addType")
    @SysTemLog(operModul = "系统管理-字典管理", operType = "新增", operDesc = "字典管理新增功能", modular = "PC")
    public ResultUtil<T> addDictType(HttpServletRequest request, @RequestBody SysDictType type) {
        ResultUtil<T> res = dictTypeService.checkDictTypeField(type);
        if (!(res.getCode() == 200)) {
            return res;
        }
        UserDto user = TokenUtil.getUser(request);
        type.setUserId(user.getId());
        type.setCreateTime(LocalDateTime.now());
        boolean b2 = dictTypeService.addDictType(type);
        return b2 ? ResultUtil.ok() : ResultUtil.fail();
    }

    /**
     * 字典编辑
     *
     * @param type 字典值
     * @return 编辑结果
     */
    @PostMapping("/updateType")
    @SysTemLog(operModul = "系统管理-字典管理", operType = "编辑", operDesc = "字典管理编辑功能", modular = "PC")
    public ResultUtil<T> updateDictType(HttpServletRequest request, @RequestBody SysDictType type) {
        ResultUtil<T> res = dictTypeService.checkDictTypeField(type);
        if (!(res.getCode() == 200)) {
            return res;
        }
        UserDto user = TokenUtil.getUser(request);
        type.setUpdateUserId(user.getId());
        type.setUpdateTime(LocalDateTime.now());
        boolean b2 = dictTypeService.updateDictType(type);
        return b2 ? ResultUtil.ok() : ResultUtil.fail();
    }

    /**
     * 字典唯一校验
     *
     * @param type 字典值
     */
    @PostMapping("/checkDictType")
    public ResultUtil<T> checkDictType(@RequestBody SysDictType type) {
        return dictTypeService.checkDictTypeField(type);
    }

    /**
     * 字典删除
     *
     * @param id 字典id
     */
    @RequestMapping("/deleteType/{id}")
    @SysTemLog(operModul = "系统管理-字典管理", operType = "删除", operDesc = "字典管理删除功能", modular = "PC")
    public ResultUtil<T> delDictType(@PathVariable("id") Long id) {
        boolean b2 = dictTypeService.delDict(id);
        return b2 ? ResultUtil.ok() : ResultUtil.fail();
    }

    /**
     * 字典项分页查询
     *
     * @param vo 查询字段
     */
    @RequestMapping("/getDictList")
    public ResultUtil<List<SysDictVo>> getDictList(@RequestBody SysDictVo vo) {
        Page<SysDictVo> page = new Page<>(vo.getPageIndex(), vo.getPageSize());
        List<SysDictVo> types = dictService.getSysDictList(page, vo);
        return ResultUtil.build(types, page.getTotal(), page.getPages(), ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 字典项新增
     *
     * @param dict 字典项属性值
     */
    @PostMapping("/addDict")
    @SysTemLog(operModul = "系统管理-字典管理", operType = "新增", operDesc = "字典项新增功能", modular = "PC")
    public ResultUtil<T> addDict(HttpServletRequest request, @RequestBody SysDict dict) {
        ResultUtil<T> res = dictService.checkDictField(dict);
        if (!(res.getCode() == 200)) {
            return res;
        }
        UserDto user = TokenUtil.getUser(request);
        dict.setUserId(user.getId());
        dict.setCreateTime(LocalDateTime.now());
        boolean b2 = dictService.addDict(dict);
        return b2 ? ResultUtil.ok() : ResultUtil.fail();
    }

    /**
     * 字典项编辑
     *
     * @param dict 字典项属性值
     */
    @PostMapping("/updateDict")
    @SysTemLog(operModul = "系统管理-字典管理", operType = "编辑", operDesc = "字典项编辑功能", modular = "PC")
    public ResultUtil<T> updateDict(HttpServletRequest request, @RequestBody SysDict dict) {
        ResultUtil<T> res = dictService.checkDictField(dict);
        if (!(res.getCode() == 200)) {
            return res;
        }
        UserDto user = TokenUtil.getUser(request);
        dict.setUpdateUserId(user.getId());
        dict.setUpdateTime(LocalDateTime.now());
        boolean b2 = dictService.updateDict(dict);
        return b2 ? ResultUtil.ok() : ResultUtil.fail();
    }

    /**
     * 字典项唯一校验
     *
     * @param dict 字典项属性值
     */
    @PostMapping("/checkDict")
    public ResultUtil<T> checkDict(@RequestBody SysDict dict) {
        return dictService.checkDictField(dict);
    }

    /**
     * 字典项删除
     *
     * @param id 字典项id
     */
    @RequestMapping("/deleteDict/{id}")
    @SysTemLog(operModul = "系统管理-字典管理", operType = "删除", operDesc = "字典项删除功能", modular = "PC")
    public ResultUtil<T> deleteDict(@PathVariable("id") Long id) {
        boolean b2 = dictService.deleteDict(id);
        return b2 ? ResultUtil.ok() : ResultUtil.fail();
    }


    /**
     * 根据字典类型获取字典项
     *
     * @param sysDictVo 字典类型
     */
    @RequestMapping("/getSysDictByType")
    public ResultUtil<List<SysDictVo>> getSysDictByType(@RequestBody SysDictVo sysDictVo) {
        // 创建分页对象
        Page<SysDictVo> page = new Page<>(sysDictVo.getPageIndex(), sysDictVo.getPageSize());
        List<SysDictVo> sysDictList = dictService.getSysDictList(page, sysDictVo);
        return ResultUtil.build(sysDictList,page.getTotal(),page.getPages(), ResultUtilCodeEnum.SUCCESS);
    }
}

